package com.kkd.kkdapp.adapter;

import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.MyHead;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.view.RoundImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HeadGridViewAdapter extends BaseAdapter {

	private Context context;
	private List<MyHead> arr;

	public HeadGridViewAdapter(Context context, List<MyHead> arr) {
		this.context = context;
		this.arr = arr;
	}

	@Override
	public int getCount() {
		return arr.size();
	}

	@Override
	public Object getItem(int position) {
		return arr.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder mViewHolder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.headgridview_item,
					null);
			mViewHolder = new ViewHolder();
			mViewHolder.line_view = convertView.findViewById(R.id.line_view);
			mViewHolder.happy_ll = (LinearLayout) convertView
					.findViewById(R.id.happy_ll);
			mViewHolder.mButton = (Button) convertView
					.findViewById(R.id.button);
			mViewHolder.head = (RoundImageView) convertView
					.findViewById(R.id.head);
			mViewHolder.happy_tv = (TextView) convertView
					.findViewById(R.id.happy_tv);
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		// 分割线
		mViewHolder.line_view.setVisibility(View.GONE);
		if ((position + 1) % 2 == 0) {
			mViewHolder.line_view.setVisibility(View.VISIBLE);
		}
		// 快乐币布局
		mViewHolder.happy_ll.setVisibility(View.VISIBLE);
		// 兑换或设置头像按钮
		mViewHolder.mButton.setText("兑换");
		mViewHolder.mButton.setBackgroundResource(R.drawable.button_orange);
		// 头像
		ImageLoader.getInstance().displayImage(arr.get(position).getImageUrl(),
				mViewHolder.head, R.drawable.head_portrait);
		mViewHolder.mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (callback != null) {
					callback.posion(position, mViewHolder.mButton.getText()
							.toString());
				}
			}
		});
		int status = arr.get(position).getStatus();
		mViewHolder.mButton.setClickable(true);
		if (status == 2) {
			mViewHolder.happy_ll.setVisibility(View.GONE);
			mViewHolder.mButton.setText("设为头像");
			mViewHolder.mButton.setBackgroundResource(R.drawable.button_violet);
		} else if (status == 1) {
			mViewHolder.happy_ll.setVisibility(View.GONE);
			mViewHolder.mButton.setText("设为头像");
			mViewHolder.mButton.setBackgroundResource(R.drawable.button_gray);
			mViewHolder.mButton.setClickable(false);
		}
		mViewHolder.mButton.setPadding(1, 1, 1, 1);
		// 快乐币兑换个数
		mViewHolder.happy_tv.setText(arr.get(position).getCoinCount() + "个");
		return convertView;
	}

	private class ViewHolder {
		private View line_view;
		private LinearLayout happy_ll;
		private Button mButton;
		private RoundImageView head;
		private TextView happy_tv;
	}

	public void setGridViewItemListenerr(GridViewItemListener callback) {
		this.callback = callback;
	}

	public GridViewItemListener callback;

	public interface GridViewItemListener {
		public void posion(int posion, String text);
	}
}
